package org.lwjgl.demo.geometries;

/**
 * 几何体基类
 *
 * @author zhoudy
 * @date 2025/10/29 21:52
 */
import org.lwjgl.system.MemoryStack;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import static org.lwjgl.opengl.GL30.*;

public abstract class Mesh {
    protected int vao;
    protected int vbo;
    protected int ebo;
    protected int vertexCount;

    // ==================== 新增：纹理相关属性 ====================
    protected int diffuseTexture = 0;
    protected int specularTexture = 0;

    public abstract void render();

    protected void setupMesh(float[] vertices, int[] indices) {
        try (MemoryStack stack = MemoryStack.stackPush()) {
            vao = glGenVertexArrays();
            vbo = glGenBuffers();
            ebo = glGenBuffers();

            glBindVertexArray(vao);

            FloatBuffer vertexBuffer = stack.mallocFloat(vertices.length);
            vertexBuffer.put(vertices).flip();

            glBindBuffer(GL_ARRAY_BUFFER, vbo);
            glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);

            IntBuffer indexBuffer = stack.mallocInt(indices.length);
            indexBuffer.put(indices).flip();

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW);

//            // 位置属性
//            glVertexAttribPointer(0, 3, GL_FLOAT, false, 6 * Float.BYTES, 0);
//            glEnableVertexAttribArray(0);
//            // 颜色属性
//            glVertexAttribPointer(1, 3, GL_FLOAT, false, 6 * Float.BYTES, 3 * Float.BYTES);
//            glEnableVertexAttribArray(1);

            // ==================== 确认：正确的顶点属性布局 ====================
            // 位置属性 (0) - 3个float
            glVertexAttribPointer(0, 3, GL_FLOAT, false, 8 * Float.BYTES, 0);
            glEnableVertexAttribArray(0);

            // 法线属性 (1) - 3个float
            glVertexAttribPointer(1, 3, GL_FLOAT, false, 8 * Float.BYTES, 3 * Float.BYTES);
            glEnableVertexAttribArray(1);

            // 纹理坐标属性 (2) - 2个float
            glVertexAttribPointer(2, 2, GL_FLOAT, false, 8 * Float.BYTES, 6 * Float.BYTES);
            glEnableVertexAttribArray(2);


            glBindVertexArray(0);

            vertexCount = indices.length;

            System.out.println("立方体顶点数: " + (vertices.length / 8) + ", 三角形数: " + (indices.length / 3));
        }
    }

    // ==================== 新增：纹理设置和获取方法 ====================
    public void setDiffuseTexture(int textureId) {
        this.diffuseTexture = textureId;
    }

    public void setSpecularTexture(int textureId) {
        this.specularTexture = textureId;
    }

    public int getDiffuseTexture() {
        return diffuseTexture;
    }

    public int getSpecularTexture() {
        return specularTexture;
    }

    // ==================== 新增：检查是否有纹理的方法 ====================
    public boolean hasDiffuseTexture() {
        return diffuseTexture != 0;
    }

    public boolean hasSpecularTexture() {
        return specularTexture != 0;
    }
    public void cleanup() {
        glDeleteVertexArrays(vao);
        glDeleteBuffers(vbo);
        glDeleteBuffers(ebo);

        // ==================== 新增：清理纹理资源 ====================
        if (diffuseTexture != 0) {
            glDeleteTextures(diffuseTexture);
        }
        if (specularTexture != 0) {
            glDeleteTextures(specularTexture);
        }

    }
}
